﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Core;
using Excel = Microsoft.Office.Interop.Excel;


namespace prototipo1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void LPN(long lpn)
        {
            for(int i=0; i<dataGridView1.Rows.Count-1;i++)
                dataGridView1[7, i].Value = (lpn+i).ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            StreamReader lector;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.InitialDirectory = "c:\\Users/kuragaroth/Desktop/sodimac";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string texto;
                    int count = 29;
                    string[] split = null;

                    try
                    { 
                        StreamReader tr = new StreamReader(openFileDialog1.FileName);
                        while ((texto = tr.ReadLine()) != null){
                            split = texto.Split(new Char[] {'|'}, count);
                            dataGridView1.Rows.Add(split[15], split[16], split[17], split[22], split[23], split[24], split[25], split[26]);
                            split = null;
                        }
                        dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
                       
                        MessageBox.Show("El archivo se cargo correctamente", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
               
                    }catch (Exception ex){
                        MessageBox.Show(ex.Message);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: No se pudo leer el archivo del disco. Error original: " + ex.Message);
                }
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex == dataGridView1.Rows.Count-1)
            {

                dataGridView1.Rows.Insert(e.RowIndex, dataGridView1[0, e.RowIndex].Value, dataGridView1[1, e.RowIndex].Value, dataGridView1[2, e.RowIndex].Value, dataGridView1[3, e.RowIndex].Value, dataGridView1[4, e.RowIndex].Value, dataGridView1[5, e.RowIndex].Value, dataGridView1[6, e.RowIndex].Value, dataGridView1[7, e.RowIndex].Value);
            }
            else {
                dataGridView1.Rows.Insert(e.RowIndex + 1, dataGridView1[0, e.RowIndex].Value, dataGridView1[1, e.RowIndex].Value, dataGridView1[2, e.RowIndex].Value, dataGridView1[3, e.RowIndex].Value, dataGridView1[4, e.RowIndex].Value, dataGridView1[5, e.RowIndex].Value, dataGridView1[6, e.RowIndex].Value, dataGridView1[7, e.RowIndex].Value);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            LPN(Int64.Parse(textBox1.Text));
            button3.Enabled = true;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.TextLength == 16)
            {
                button2.Enabled = true;
            }
            else {
                button2.Enabled = false;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.InitialDirectory = "c:\\Users/kuragaroth/Desktop/sodimac";
            openFileDialog1.Filter = "archivo excel (*.xlsx*)|*.xlsx|archivo excel (*.xls)|*.xls";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {


               Excel.Application myExcelApp;
               Excel.Workbooks myExcelWorkbooks;
               Excel.Workbook myExcelWorkbook;


               object misValue = System.Reflection.Missing.Value;


                myExcelApp = new Excel.Application();
                myExcelApp.Visible = true;
                myExcelWorkbooks = myExcelApp.Workbooks;
                String fileName = openFileDialog1.FileName; 
                myExcelWorkbook = myExcelWorkbooks.Open(fileName, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);                 

                Excel.Worksheet myExcelWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;
 
                //String cellFormulaAsString = myExcelWorksheet.get_Range("A2", misValue).Formula.ToString();


                int num = 7;

                for (int i = 0; i < dataGridView1.Rows.Count; i++) {
                    myExcelWorksheet.get_Range("A"+(num+i), misValue).Formula = dataGridView1[0, i].Value;
                }
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    myExcelWorksheet.get_Range("B" + (num + i), misValue).Formula = dataGridView1[1, i].Value;
                }
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    myExcelWorksheet.get_Range("C" + (num + i), misValue).Formula = dataGridView1[2, i].Value;
                }
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    myExcelWorksheet.get_Range("D" + (num + i), misValue).Formula = dataGridView1[3, i].Value;
                }
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    myExcelWorksheet.get_Range("I" + (num + i), misValue).Formula = dataGridView1[5, i].Value;
                }
                for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                {
                    myExcelWorksheet.get_Range("K" + (num + i), misValue).Formula = dataGridView1[7, i].Value;
                }
             }
             catch (Exception ex)
             {
                    MessageBox.Show("Error: No se pudo leer el archivo del disco. Error original: " + ex.Message);
             }
            }
        }
    }
}
